package com.ruoyi.platform.system.menu.pojo;

import com.ruoyi.platform.system.menu.enums.MenuEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 菜单权限表
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-16 02:15:03
 */
@ApiModel
@Data
@NoArgsConstructor
public class MenuTreeDTO {
    /**
     * 菜单code
     */
    @ApiModelProperty("菜单code")
    private String menuCode;
    /**
     * 菜单名称
     */
    @ApiModelProperty("菜单名称")
    private String menuName;
    /**
     * 父菜单Code
     */
    @ApiModelProperty("父菜单Code")
    private String parentCode;
    /**
     * 菜单类型（1目录2菜单3按钮）
     */
    @ApiModelProperty("菜单类型（1目录2菜单3按钮）")
    private Integer menuType;
    /**
     * 路由
     */
    @ApiModelProperty("路由")
    private String path;
    /**
     * 子菜单
     */
    @ApiModelProperty("子菜单")
    private List<MenuTreeDTO> children;
    /**
     * 是否有权限
     */
    @ApiModelProperty("是否有权限1是0否")
    private Integer isPermission;

    public MenuTreeDTO(MenuEnum menu) {
        this.menuCode = menu.getCode();
        this.menuName = menu.getMenuName();
        this.parentCode = menu.getParentCode();
        this.menuType = menu.getMenuType();
        this.path = menu.getPath();
    }

    public MenuTreeDTO(MenuEnum menu, List<MenuEnum> permitMenuList) {
        this.menuCode = menu.getCode();
        this.menuName = menu.getMenuName();
        this.parentCode = menu.getParentCode();
        this.menuType = menu.getMenuType();
        this.path = menu.getPath();
        if (CollectionUtils.isNotEmpty(permitMenuList)) {
            permitMenuList.stream().filter(x -> Objects.equals(this.menuCode, x.getCode()))
                    .findAny().ifPresent(x -> this.isPermission = 1);
        }
    }
}